package xrz.com.jr;

import android.app.LocalActivityManager;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.widget.RadioButton;
import android.widget.RadioGroup;

import java.util.ArrayList;
import java.util.List;

import xrz.com.jr.adapter.ViewPagerAdapter;
import xrz.com.jr.utils.ExitUtil;

/**
 * Created by Administrator on 2016/7/29.
 */
public class TabMainActivity extends BaseActivity {
    public static ViewPager mViewPager;
    private RadioGroup mRgp;
    private RadioButton Rbt1;
    private RadioButton Rbt2;
    private RadioButton Rbt3;
    private LocalActivityManager manager = null;
    private ViewPagerAdapter adapter;
    private List<View> views;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        manager = new LocalActivityManager(this, true);
        manager.dispatchCreate(savedInstanceState);
        setContentView(R.layout.layout_tab);
        ExitUtil.getInstant().addToPool(this);
        init();
    }

    private View getView(String id, Intent intent) {
        return manager.startActivity(id, intent).getDecorView();
    }

    private void initView() {
        mViewPager = (ViewPager) findViewById(R.id.viewpager);
        mRgp = (RadioGroup) findViewById(R.id.rgp);
        Rbt1 = (RadioButton) findViewById(R.id.rb1);
        Rbt2 = (RadioButton) findViewById(R.id.rb2);
        Rbt3 = (RadioButton) findViewById(R.id.rb3);

    }

    private void init() {
        initView();
        views = new ArrayList<View>();
        Intent i1 = new Intent(this, MainActivity.class);
        views.add(getView("A", i1));
        Intent i2 = new Intent(this, XitongXueXiActivity.class);
        views.add(getView("B", i2));
        Intent i3 = new Intent(this, KeFuActivity.class);
        views.add(getView("c", i3));
        adapter = new ViewPagerAdapter(views);
        mViewPager.setAdapter(adapter);
        mViewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                switch (position) {
                    case 0:
                        Rbt1.setChecked(true);
                        break;
                    case 1:
                        Rbt2.setChecked(true);
                        break;
                    case 2:
                        Rbt3.setChecked(true);
                        break;
                }
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
        mRgp.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup radioGroup, int i) {
                switch (i) {
                    case R.id.rb1:
                        mViewPager.setCurrentItem(0);
                        break;
                    case R.id.rb2:
                        mViewPager.setCurrentItem(1);
                        break;
                    case R.id.rb3:
                        mViewPager.setCurrentItem(2);
                        break;
                }
            }
        });
    }
}
